package com.example.universita.esami;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.MenuItem.OnMenuItemClickListener;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.widget.TextView;

public class Dettagli_esame extends Activity  {
	private static final int NUM_OF_COLS=7;
	private static final int POS_LODE=8;
	private static final int POS_IDO=9;	
	
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dettagli_esame);
        Intent intent = getIntent();
        String pkg = getPackageName();
        String str = intent.getStringExtra(pkg+".myString");
        DBDataUniversity db = new DBDataUniversity(this);
        db.open();
        Cursor cursor = db.getExamsDetails( str );
		
		TextView tv=null;
		
		
		cursor.moveToNext();
			
		for(int i=0; i<=NUM_OF_COLS; i++){
			
			str=cursor.getString(i);
			if(str==null)
				continue;
			tv = (TextView)findViewById(R.id.text_dettagli_nome+i);
	        tv.append(str);
	        
		}
		
		tv.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				Intent intent = new Intent(getBaseContext(), Docenti.class);
	    		TextView tv = (TextView) findViewById(R.id.text_dettagli_docente);
	    		String pkg = getPackageName();
	    		intent.putExtra(pkg+".Docente", tv.getText().toString());
	    		startActivity(intent);
			}
		});
		
		if(cursor.getInt(POS_LODE)!=0){
			tv = (TextView) findViewById(R.id.text_dettagli_voto);	
			tv.setText("30L");
		}		
		
		if(cursor.getInt(POS_IDO)!=0){
			tv = (TextView) findViewById(R.id.text_dettagli_voto);
			if(tv.getText()!="0")
				tv.setText("IDO");
		}	
		
		db.close();
    }
	
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_dettagli_esame, menu);
        
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item){
    	Intent intent;
    	String pkg;
    	switch(item.getItemId()){
    	case R.id.dettagli: // Vai a Dettagli Esame
	    	   	intent = new Intent (this, Details.class);
			   	startActivity(intent);
			   	break; 
    	case R.id.note: // Vai a Dettagli Esame
    	   		intent = new Intent (this, Note.class);
    	    	TextView tv1 = (TextView) findViewById(R.id.text_dettagli_nome);
    	    	pkg = getPackageName();
	    		intent.putExtra(pkg+".myString", tv1.getText().toString());
    	   		startActivity(intent);
    	   		break;
    	case R.id.voto: // Vai a Dettagli Esame
    	   		intent = new Intent (this, Inserimento_voti.class);
    	    	TextView tv2 = (TextView) findViewById(R.id.text_dettagli_nome);
    	    	pkg = getPackageName();
	    		intent.putExtra(pkg+".myString", tv2.getText().toString());
    	   		startActivity(intent);
    	   		break;    		
	    case R.id.carriera: // Vai a Carriera
	    	   	intent = new Intent(this, Carriera.class);
	    	   	startActivity(intent);  
	    	   	break;             
	    case R.id.home: // Vai alla Home
  	   	   		intent = new Intent(this, MainActivity.class);
  	   	   		startActivity(intent);  
  	   	   		break;    
	    case R.id.statistiche: // Vai alla Home
	  	   	   	intent = new Intent(this, StatisticActivity.class);
	  	   	   	startActivity(intent);  
	  	   	   	break;    
	    case R.id.modifica: // Vai a Dettagli Esame
    	    	intent = new Intent(getBaseContext(), EditEsame.class);
    	    	TextView tv3 = (TextView) findViewById(R.id.text_dettagli_nome);
    	    	pkg = getPackageName();
				intent.putExtra(pkg+".nomeEsame", tv3.getText().toString() );
				intent.putExtra(pkg+".daDove", true);
				startActivity(intent);
				break;       
    	}
     return true;
    }

}
